package cn.bupt.adt.controller.inf;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.Recharge;
import cn.bupt.adt.service.IDoctorAppUserService;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.service.IRechargeService;
import cn.bupt.adt.util.MD5Util;
import cn.bupt.adt.util.NetDataAccessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/interface/recharge")
public class RechargeController {

    @Autowired
    IRechargeService rechargeService;
    @Autowired
    IDoctorAppUserService doctorAppUserService;

    @Autowired
    IInstitutionAppUserService institutionAppUserService;

    @RequestMapping(value = "/doctorRecharge", method = RequestMethod.POST)
    @ResponseBody
    //医生充值需要token，以及充值的金额money
    public Object doctorRecharge(Recharge recharge, HttpServletRequest request) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        recharge.setTime(new Date());
        recharge.setFlag(Constants.DOC_CHARGE);
        recharge.setUserId(userId);
        recharge.setStatus(Constants.UNPAY);
        recharge.setVerifyCode(MD5Util.MD5(String.valueOf(recharge.getTime().getTime())));
        Map<String, Object> content = new HashMap<>();
        if (rechargeService.addRecharge(recharge)) {
            recharge = rechargeService.getRechargeById(recharge.getId());
            content.put("rechargeId", recharge.getId());
            content.put("money", recharge.getMoney());
            content.put("verifyCode", recharge.getVerifyCode());
            nau.setContent(content);
            nau.setResult(1);
            nau.setResultDesp("添加充值记录成功，请完成支付");
        } else {
            nau.setResult(0);
            nau.setResultDesp("因网络等其他原因未能添加充值记录，请稍后再尝试");
        }
        return nau;
    }

    @RequestMapping(value = "/institutionRecharge", method = RequestMethod.POST)
    @ResponseBody
    //机构充值需要token，以及充值的金额money
    public Object institutionRecharge(Recharge recharge, HttpServletRequest request) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        Integer userId = (Integer) request.getAttribute("userId");
        recharge.setTime(new Date());
        recharge.setFlag(Constants.INS_CHARGE);
        recharge.setUserId(userId);
        recharge.setStatus(Constants.UNPAY);
        recharge.setVerifyCode(MD5Util.MD5(String.valueOf(recharge.getTime().getTime())));
        Map<String, Object> content = new HashMap<>();
        if (rechargeService.addRecharge(recharge)) {
            recharge = rechargeService.getRechargeById(recharge.getId());
            content.put("rechargeId", recharge.getId());
            content.put("money", recharge.getMoney());
            content.put("verifyCode", recharge.getVerifyCode());
            nau.setContent(content);
            nau.setResult(1);
            nau.setResultDesp("添加充值记录成功，请完成支付");
        } else {
            nau.setResult(0);
            nau.setResultDesp("因网络等其他原因未能添加充值记录，请稍后再尝试");

        }
        return nau;
    }
}
